package a66weding.com.jiehuntong.activity.toolsbudget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.common.utils.CUtils;

public class ToolsBudgetEditbudgetActivity extends BaseBudgetActivity implements View.OnClickListener {
    private TextView mCityTv;
    private ImageView mDownArrIv;
    private TextView mTitleTv;
    private TextView mMoneyCountTv;
    private ListView mBudgetLview;
    private LinearLayout mBackLay;
    private ListViewAdapter mAdapter;
    private Button mAddTypeBtn;
    private EditText mTypeTitle;
    private PopupWindow mAddTypepop;
    private List<BudgetBean.DataBean> budgetList;
    private int budgetCount;
    private boolean isEdit;
    private int mCurrentSelection;
    private BudgetBean budgetBean;
    private String typeName;
    private int btnwidth;
    private int btnheight;
    private int screenheight;
    private int screenwidth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_budget_editbudget);
        initview();
        initdata();
    }
    private void initview() {
        mCityTv = (TextView) findViewById(R.id.tv_tools_wedregistry_place);
        mDownArrIv = (ImageView) findViewById(R.id.iv_head_downarr);
        mTitleTv = (TextView) findViewById(R.id.tv_tools_dayplan_title);
        mBackLay = (LinearLayout) findViewById(R.id.llay_tools_wedregister_back);
        mMoneyCountTv = (TextView) findViewById(R.id.tv_tools_budgetmoney);
        mBudgetLview = (ListView) findViewById(R.id.lview_tools_budget);
        mAddTypeBtn = (Button) findViewById(R.id.btn_tools_budget_addtype);
        mCityTv.setText("完成");
        mTitleTv.setText("修改结婚预算");
        mDownArrIv.setVisibility(View.GONE);
        mCityTv.setOnClickListener(this);
        mBackLay.setOnClickListener(this);
        mAddTypeBtn.setOnClickListener(this);

    }
    private void initdata() {
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        budgetBean = (BudgetBean) bundle.getSerializable("budgetBean");
        if(budgetBean!=null) {
        }
        budgetCount= budgetBean.getMoneyCount();
        mMoneyCountTv.setText(""+budgetCount);
        if(budgetBean !=null) {
            budgetList = budgetBean.getData();
            if(budgetList!=null && budgetList.size()>0) {
                mAdapter=new ListViewAdapter();
                mBudgetLview.setAdapter(mAdapter);
                mBudgetLview.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
                        switch(scrollState){
                        }
                    }
                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    }
                });

            }
        }
        btnwidth = mAddTypeBtn.getWidth();
        btnheight = mAddTypeBtn.getHeight();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wm.getDefaultDisplay();
        screenheight = defaultDisplay.getHeight();
        screenwidth = defaultDisplay.getWidth();

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llay_tools_wedregister_back://返回
                ToolsBudgetActivity.ispopShow=true;
                finish();
                break;
            case R.id.btn_tools_budget_addtype://添加类型
                showDialog(view);
                break;
            case R.id.confirm://确定添加类型
                typeName = mTypeTitle.getText().toString().trim();
                if(TextUtils.isEmpty(typeName)) {
                    CUtils.showToast("类型名称不能为空！");
                    return;
                }
                addTypetoBean(typeName);
                ToolsBudgetActivity.isNeedSave=true;
                if(mAddTypepop!=null) {
                    mAddTypepop.dismiss();
                }
                break;
            case R.id.cancel://取消添加类型
                if(mAddTypepop!=null) {
                    mAddTypepop.dismiss();
                }

                break;
            case R.id.tv_tools_wedregistry_place://保存
                int moneyAccount=0;
                for (int i = 0; i < budgetList.size(); i++) {
                    moneyAccount += budgetList.get(i).getWBItemAmount();
                }
                budgetBean.setMoneyCount(moneyAccount);
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("budgetbean",budgetBean);
                intent.putExtras(bundle);
                setResult(GETBUDGETRESULTCODE, intent);
                ToolsBudgetActivity.ispopShow=true;
                ToolsBudgetActivity.isNeedSave=true;
                finish();
                break;
        }

    }




    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return budgetList.size();
        }

        @Override
        public Object getItem(int position) {
            return budgetList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final BudgetBean.DataBean dataBean = budgetList.get(position);

            ViewHolder myHolder=null;

            myHolder=new ViewHolder();
            convertView=View.inflate(ToolsBudgetEditbudgetActivity.this,R.layout.item_tools_budget_editcontentitem,null);

            myHolder.mNameTv= (TextView) convertView.findViewById(R.id.name);
            myHolder.mMoneyEt= (EditText) convertView.findViewById(R.id.et_tools_budgetmoney);


            myHolder.mNameTv.setText(dataBean.getWBItemName());
            myHolder.mMoneyEt.setHint("0");
            myHolder.mMoneyEt.setText(""+dataBean.getWBItemAmount());

            myHolder.mMoneyEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    String money=s.toString().trim();


                    if(TextUtils.isEmpty(money)) {
                        dataBean.setWBItemAmount(0);
                    }else{
                        if (money.length() > 9) {
                            Toast.makeText(ToolsBudgetEditbudgetActivity.this,"您输入的金额过大,请重新输入",Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            dataBean.setWBItemAmount(Integer.valueOf(money));
                        }
                        int moneyAccount=0;
                        for (int i = 0; i < budgetList.size(); i++) {
                            moneyAccount += budgetList.get(i).getWBItemAmount();
                        }
                        mMoneyCountTv.setText(""+moneyAccount);
                    }
                }
            });

            myHolder.mMoneyEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    mCurrentSelection=position;
                }
            });

            if(mCurrentSelection==position) {
                myHolder.mMoneyEt.requestFocus();
            }

            return convertView;
        }

        class ViewHolder {
            EditText mMoneyEt;
            TextView mNameTv;

        }
    }

    public void startAnimation(View view,boolean isreturn){
        Animation mAnima=null;
        if(isreturn) {
            int height = view.getHeight();
            int width = view.getWidth();
            mAnima=new TranslateAnimation(0,btnwidth,screenheight,btnheight);
            mAnima.setDuration(1000);
        }else{
            mAnima=new TranslateAnimation(btnwidth,0,btnheight,screenheight);
            mAnima.setDuration(1000);
        }
        view.startAnimation(mAnima);
    }

    public  void showDialog(View view) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.confirm_dialog_add, null);
        mTypeTitle = (EditText) contentView.findViewById(R.id.title);
        TextView cancelTv = (TextView) contentView.findViewById(R.id.cancel);
        TextView confirmTv = (TextView) contentView.findViewById(R.id.confirm);

        cancelTv.setOnClickListener(ToolsBudgetEditbudgetActivity.this);
        confirmTv.setOnClickListener(ToolsBudgetEditbudgetActivity.this);

        mAddTypepop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        //设置位置
        mAddTypepop.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        //点击空白处时，隐藏掉pop窗口
        mAddTypepop.setTouchable(false);

        //在PopupWindow里面就加上下面代码，让键盘弹出时，不会挡住pop窗口。
        mAddTypepop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mAddTypepop.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });
    }

    /**
     * 添加类型
     * @param typeName
     */
    private void addTypetoBean(String typeName) {
        BudgetBean.DataBean dataBean = new BudgetBean.DataBean();
        dataBean.setWBItemName(typeName);
        dataBean.setWBItemAmount(0);
        dataBean.setPersent(0);

        budgetList.add(dataBean);

        if(mAdapter!=null) {
            mAdapter.notifyDataSetChanged();
        }



    }
}
